package com.chia.multienty.workflow.delegate;

import com.chia.multienty.core.domain.enums.FlowableVariables;
import com.chia.multienty.core.util.SpringUtil;
import lombok.SneakyThrows;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

@Component
public class DynamicInvokeServiceTask implements JavaDelegate {
    @Override
    @SneakyThrows
    public void execute(DelegateExecution execution) {
        String className = execution.getVariable(FlowableVariables.OUT_SERVICE_CLASS_NAME.name(), String.class);
        String methodName = execution.getVariable(FlowableVariables.OUT_METHOD_NAME.name(), String.class);
        Class<?> clazz = Class.forName(className);
        Object bean = SpringUtil.getBean(clazz);
        Method method = bean.getClass().getDeclaredMethod(methodName, Map.class);
        method.invoke(method, execution.getVariables());
    }
}
